﻿using System;
using System.Linq;
using ExcelDna.Integration;
using Exocortex.Base.Shared;
using Exocortex.Excel.Shared;
using Helpers = Exocortex.Base.Shared.Helpers;

namespace Exocortex.Excel.Nuget
{

    public class NugetClientAddIn : IExcelAddIn
    {
        // Nlog
        private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        public static ClientManager<WebProjectManager> NugetFeedManager = new ClientManager<WebProjectManager>();

        #region Implementation of IExcelAddIn

        public void AutoOpen()
        {
            try
            {
                _logger.Info(() => "Starting Nuget Add-In");
                _logger.Info(() => String.Format("Loading Nuget Add-In config file {0}...", Helpers.DefaultConfigFileName));
                _logger.Info(() => "Loading Nuget Add-In config section...");
                var configSection = Helpers.CortexConfigSection();

                foreach (var item in configSection.Servers.Cast<NugetConfigurationElement>())
                    NugetFunctions.CreateFeed(item.Alias, item.Address, item.IsDefault);

                _logger.Info(() => "Checking default feed for new Exocortex version...");
                NugetFunctions.AutoUpdate();

                _logger.Info(() => "Finished loading Nuget Add-In");                
            } catch (Exception e)
            {
                _logger.FatalException("Could not complete load for Nuget Add-In", e);
            }
        }

        public void AutoClose() {}

        #endregion

    }
}
